package org.example.custom;

import org.example.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.UUID;

public class Hello02Insert {
    public static void main(String[] args) throws SQLException {
        //获取数据库连接
        Connection connection = JDBCUtil.getConnection();
        String sql = "insert into yjxxt.user values(?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        //设置批处理
        for (int i = 0; i < 10000; i++) {
            statement.setInt(1, (int) (Math.random() * 9000000 + 1000000));
            statement.setString(2, UUID.randomUUID().toString().substring(0, 8));
            statement.setString(3, "123456");
            long time = System.currentTimeMillis() - (long) (Math.random() *
                    1000L * 60 * 60 * 24 * 365);
            statement.setTimestamp(4, new Timestamp(time));
            //将本次操作假如到批中
            statement.addBatch();
        }
        //执行批处理
        int[] ints = statement.executeBatch();
        //关闭连接
        JDBCUtil.closeAll(connection, statement, null);
    }
}
